Generate Paging – Custom function

Posted on October 20 2009 by Sachin Jain

Here it goes

private int mstrPageSize = 1;
private int mstrTotalRecords = 1;
private int mstrCurrentPage = 1;

public int PageSize
{
get
{
return mstrPageSize;
}
set
{
mstrPageSize = value;
}
}

public int TotalRecords
{
get
{
return mstrTotalRecords;
}
set
{
mstrTotalRecords = value;
}
}

public int CurrentPage
{
get
{
return mstrCurrentPage;
}
set
{
mstrCurrentPage = value;
}
}
public string GeneratePaging()
{
//generate querystring

string strQuery = “”;
if (HttpContext.Current.Request["txtKeyword"] != null)
{
if (HttpContext.Current.Request["txtKeyword"].ToString() != “”)
{
strQuery += “&txtKeyword=” + HttpContext.Current.Request["txtKeyword"].ToString().Trim();
}
}

if (HttpContext.Current.Request["ModelName"] != null)
{
if (HttpContext.Current.Request["ModelName"].ToString() != “”)
{
strQuery += “&ModelName=” + HttpContext.Current.Request["ModelName"].ToString().Trim();
}
}

}
//generate querystring

string strPageHtml = “”;
int intNoOfPages = mstrTotalRecords / mstrPageSize;
if (mstrTotalRecords > mstrPageSize && (mstrTotalRecords % mstrPageSize != 0))
{
intNoOfPages++;
}
int intPageLimit = 9;
int intPageDifference = 5;
int intMinPage = 1;
int intMaxPage = 10;
if (intMaxPage > intNoOfPages)
{
intMaxPage = intNoOfPages;
}
if (mstrCurrentPage > intPageLimit)
{

if (mstrCurrentPage < intNoOfPages)
{
intMinPage = mstrCurrentPage - intPageDifference;
if ((mstrCurrentPage + intPageDifference) > intNoOfPages)
{
intMaxPage = intNoOfPages;
}
else
{

intMaxPage = mstrCurrentPage + intPageDifference;
}
}
else
{
intMinPage = mstrCurrentPage – intPageLimit;
intMaxPage = intNoOfPages;
}
}

for (int intPage = intMinPage; intPage <= intMaxPage; intPage++)
{

if (intPage == intMinPage)
{
if (mstrCurrentPage > 1)
{
strPageHtml += “First “;
strPageHtml += “Previous “;
}
else
{
strPageHtml += ” First “;
strPageHtml += ” Previous “;
}
}
if (intPage == mstrCurrentPage)
{
strPageHtml += ” [" + intPage + "] “;
}
else
{
strPageHtml += “” + intPage + “ “;

}
if (intPage == intMaxPage)
{
if (mstrCurrentPage < intNoOfPages)
{
strPageHtml += " Next “;
strPageHtml += ” Last “;
}
else
{
strPageHtml += ” Next “;
strPageHtml += ” Last “;
}
}

}

return strPageHtml;
}

VN:F [1.9.0_1079]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.0_1079]
Rating: 0 (from 0 votes)

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Powered by Sachin Jain

Powered by Olark